Import yimWork

Class yimParticleEmitter
	Field freq:Int
	Field num:Int
	Field spr:yimSprite
	Field nextEmit:Int = 0
	Field ang:Float
	Field off:yimVec2D
	Field vel:Float
	Field collG:yimCollGroup
	Field collRadius:Float
	Field angVariance:Float = 45.0
	Field particleLife:Float = 2000
	Field deathTime:Int = -1
	Field lockToSpr:yimSprite = Null
	Field bodyOffX:Float = 0
	Field bodyOffY:Float = 0
	Field gravity:Float = 0.0
	
	Method New(_spr:yimSprite, emitFreq:Int, emitNum:Int, offX:Float=0, offY:Float=0, angle:Float=0, _vel:Float=0, collGroup:yimCollGroup=Null, bodyRadius:Float=0)
		spr = _spr
		freq = emitFreq
		num = emitNum
		
		off = New yimVec2D(offX, offY)
		ang = angle
		vel = _vel
				
		collG = collGroup
		collRadius = bodyRadius
		
	End
	
	Method Update(x:Float, y:Float, time:Float, grav:Float)
		If lockToSpr
			x = lockToSpr.pos.x
			y = lockToSpr.pos.y
		EndIf
		
		If spr.parent.parent.eng.screenRect.IsPointIn(spr.parent.parent.eng.MapToScreenX(x + off.x, y + off.y), spr.parent.parent.eng.MapToScreenY(x + off.x, y + off.y))
			If time >= nextEmit
				For Local i:= 0 Until num
					Local tSpr:yimSprite = spr.parent.parent.eng.CloneSprite(spr)
					Local tAng:Float = ang + Rnd(-angVariance, angVariance)
					
					tSpr.SetSpeedXY(Sin(tAng-180)*vel, Cos(tAng-180)*vel)
	
					tSpr.SetPosition(x+off.x, y+off.y)
					If gravity <> 0.0
						tSpr.SetGravity(gravity)
					Else
						tSpr.SetGravity(grav)
					EndIf
					tSpr.SetTimeOfDeath(time+particleLife)
					tSpr.delayedDel = True
					If collG
						collG.AddBody(tSpr, New yimCollBodyTypeCircle(collRadius, bodyOffX, bodyOffY))	
					EndIf
				Next
				nextEmit = time+freq
			EndIf
		
		EndIf
	End
	
	Method SetPosition(x:Float, y:Float)
		off.Set(x,y)
	End
End